package com.wty.good.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.wty.commons.id.IdWorker;
import com.wty.commons.result.ResultCode;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:wangtianyu
 * @Date：Created on 2020/4/20 0020 20:52
 */
@Configuration
@MapperScan("com.wty.good.mapper")
@EnableSwagger2
@EnableTransactionManagement // 开启事务
public class EmpConfigWty {

    /**
     * 托管 idWorker
     *
     * @return
     */
    @Bean
    public IdWorker idWorker() {
        return new IdWorker(0, 0);
    }

    /**
     * 分页
     * @return
     */
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

    @Bean
    public Docket docket() {
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder().code(ResultCode.OK.code()).message(ResultCode.OK.message()).build());
        responseMessageList.add(new ResponseMessageBuilder().code(ResultCode.SYS_ERROR.code()).message(ResultCode.SYS_ERROR.message()).build());
        responseMessageList.add(new ResponseMessageBuilder().code(ResultCode.CUSTOM_ERROR.code()).message(ResultCode.CUSTOM_ERROR.message()).build());
        return new Docket(DocumentationType.SWAGGER_2)
            .pathMapping("/")// 请求的路径
            .useDefaultResponseMessages(false) // 不使用默认的返回状态码
            .globalResponseMessage(RequestMethod.GET,responseMessageList)
            .globalResponseMessage(RequestMethod.POST,responseMessageList)
            .globalResponseMessage(RequestMethod.PUT,responseMessageList)
            .globalResponseMessage(RequestMethod.DELETE,responseMessageList)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.wty.good.controller"))
            .paths(PathSelectors.any())
            .build().apiInfo(new ApiInfoBuilder()
                .title("wty-good-apis")
                .description("商品微服务restful接口")
                .version("1.0")
                .contact(new Contact("wty", "https://www.baidu.com", "1084754707@qq.com"))
                .license("The Apache License")
                .licenseUrl("http://www.baidu.com")
                .build());
    }

}
